
#include <wx/app.h>

#include "rdesktopsTaskBarIcon.h"

BEGIN_EVENT_TABLE(RDesktopsTaskBarIcon, wxTaskBarIcon)
    EVT_TASKBAR_LEFT_DOWN (RDesktopsTaskBarIcon::onLeftButtonClick)
    EVT_TASKBAR_CLICK (RDesktopsTaskBarIcon::onRightButtonClick)
END_EVENT_TABLE()

RDesktopsTaskBarIcon::RDesktopsTaskBarIcon(RDesktops* frame)
{
    this->m_frame = frame;

    wxIcon SysTrayIcon;
    SysTrayIcon.CopyFromBitmap(wxBitmap(wxImage(_T("/home/steve/rdesktops/rdesktops-gui/icons/icon.png"))));
    SetIcon(SysTrayIcon);

    m_fileMenu = new wxMenu();

    wxArrayString asFile;
    m_frame->getProfileList(asFile);

    long nMeneItemId;
    wxMenuItem* menuItem;
    for (size_t i = 0; i < asFile.GetCount(); i++)
    {
        nMeneItemId = wxNewId();
        m_profileMap[nMeneItemId] = getStdString( asFile[i] );
        menuItem = new wxMenuItem(m_fileMenu, nMeneItemId, asFile[i], asFile[i], wxITEM_NORMAL);
        m_fileMenu->Append(menuItem);
        Connect(nMeneItemId,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&RDesktopsTaskBarIcon::onMenuItemSelect);
    }

    m_popupMenu = new wxMenu();

    nMeneItemId = wxNewId();
    menuItem = new wxMenuItem(m_fileMenu, nMeneItemId, L"Main Window", L"Show Main Window", wxITEM_NORMAL);
    m_popupMenu->Append(menuItem);
    Connect(nMeneItemId,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&RDesktopsTaskBarIcon::showMainWindow);

    nMeneItemId = wxNewId();
    menuItem = new wxMenuItem(m_fileMenu, nMeneItemId, L"Quit", L"Quit", wxITEM_NORMAL);
    m_popupMenu->Append(menuItem);
    Connect(nMeneItemId,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&RDesktopsTaskBarIcon::quit);
}

void RDesktopsTaskBarIcon::showMainWindow()
{
    m_frame->Show();
}

void RDesktopsTaskBarIcon::quit()
{
    m_frame->quit();
}

void RDesktopsTaskBarIcon::onLeftButtonClick(wxTaskBarIconEvent&)
{
    PopupMenu(m_fileMenu);
}

void RDesktopsTaskBarIcon::onRightButtonClick(wxTaskBarIconEvent&)
{
    PopupMenu(m_popupMenu);
}

void RDesktopsTaskBarIcon::onMenuItemSelect(wxCommandEvent& event)
{
    m_frame->execute( m_profileMap[ event.GetId() ] );
}
